
//
//  CollectViewController.m
//  Cooking
//
//  Created by ZhiPeng.Y on 15/9/9.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "CollectViewController.h"
#import "AppMacro.h"
#import "DiaryDataBase.h"
#import "UIImageView+WebCache.h"
#import "RecommendModel.h"
#import "CollectTableViewCell.h"
#import "CookViewController.h"

@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (retain, nonatomic) NSMutableArray *collectArray;
@property (retain, nonatomic) UITableView    *collectTableView;
@property (retain, nonatomic) UILabel        *promptLabel;

@end

@implementation CollectViewController

- (void)dealloc {
    [_collectArray release];
    [_collectTableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    //判断第一次进入收藏提示,向左滑动移除
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        NSLog(@"第一次进入收藏");
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"向左滑动取消收藏" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil];
        [alterView show];
        [alterView release];
        
    } else {
            NSLog(@"不是第一次收藏");
    }
    //判断当前是否存在收藏,当未收藏内容时,弹出提示框
    [_collectArray addObjectsFromArray:[DiaryDataBase selectRecommendAll]];
    if (_collectArray.count == 0) {
        self.promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT - 49 - 64- 40, WIDTH, 40)];
        _promptLabel.textAlignment = NSTextAlignmentCenter;
        _promptLabel.text = @"您未收藏内容";
        _promptLabel.textColor = [UIColor whiteColor];
        [_promptLabel setBackgroundColor:[UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1]];
        _promptLabel.alpha = 0.0;
        [self.view addSubview:_promptLabel];
        [_promptLabel release];
        
        //动画
        [UIView beginAnimations:@"title" context:nil];
        //动画时长(持续时间)
        [UIView setAnimationDuration:3];
        //循环一次
        [UIView setAnimationRepeatCount:FP_NAN];
        [_promptLabel setFrame:CGRectMake(0, HEIGHT - 49 - 64 - 40, WIDTH, 40)];
        //透明度
        _promptLabel.alpha = 1.0;
        [UIView commitAnimations];
    }
    [_collectTableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated {
    [_collectArray removeAllObjects];
    [_promptLabel removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
    self.navigationItem.title = @"我的收藏";
    
    //左返回键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhuianniu"] style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self createCollectTableView];
}

- (void)createCollectTableView {
    self.collectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
//    _collectTableView.backgroundColor = [UIColor yellowColor];
//    _collectTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _collectTableView.delegate = self;
    _collectTableView.dataSource = self;
    [self.view addSubview:_collectTableView];
    [_collectTableView release];
    
}

#pragma mark -
#pragma mark collectTableView协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _collectArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT / 6 + 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[CollectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    RecommendModel *recommendModel = [_collectArray objectAtIndex:indexPath.row];
    [cell sendValueWithModel:recommendModel];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CookViewController *cookVC = [[CookViewController alloc] init];
    cookVC.myID = [[_collectArray objectAtIndex:indexPath.row] valueForKey:@"vegetable_id"];
//    cookVC.nameStr = [[_collectArray objectAtIndex:indexPath.row] valueForKey:@"name"];
    cookVC.recommendModel = [_collectArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:cookVC animated:YES];
    [cookVC release];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        RecommendModel *recommend = [_collectArray objectAtIndex:indexPath.row];
        [_collectArray removeObjectAtIndex:indexPath.row];
        [DiaryDataBase deleteWithRecommendId:recommend.vegetable_id];
        [_collectTableView reloadData];
    }
}

#pragma mark -
#pragma mark 左侧返回键
- (void)leftButtonAction:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
